<?php
require_once Yii::app()->basePath . "/extensions/phpquery/phpQuery/phpQuery.php";

/**
 *  前台用户控制器
 * @author xiaojy  xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class TestController extends Controller
{
    //下载图片
    public function actionUpdownImg()
    {
        $num = 1;
        $url = 'https://coinmarketcap.com/' . $num;
        //$url = 'https://www.baidu.com';
        $UserAgent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $data = curl_exec($ch);
        if ($data == false) {
            sleep(1);
        }
        curl_close($ch);
        phpQuery::newDocumentHTML($data);//加载页面
        foreach (pq("#currencies tbody tr ") as $key => $value) {
            //获取href。
            $temp = pq($value);

            $temp = pq("img", $value)->attr("src"); //图片链接

            $ext = ".png";
            $status = strripos($temp, ".png");
            if ($status != false) {
                $ext = ".png";
            }
            $status = strripos($temp, ".jpg");
            if ($status != false) {
                $ext = ".jpg";
            }
            $status = strripos($temp, ".gif");
            if ($status != false) {
                $ext = ".gif";
            }


            //下载图片
            $url = $temp;
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            $imageData = curl_exec($curl);
            $hash = md5($url);
            curl_close($curl);

            $tp = fopen("K:/png/" . $hash . $ext, "a");
            fwrite($tp, $imageData);
            fclose($tp);
        }
    }


    //下载图片
    public function actionDownImg()
    {
        $url = 'http://www.quanjing.com/category/121003/2.html';
        //$url = 'https://www.baidu.com';
        $UserAgent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $data = curl_exec($ch);
        if ($data == false) {
            slepp(1);
        }
        curl_close($ch);
        phpQuery::newDocumentHTML($data);//加载页面
        foreach (pq("li") as $key => $value) {
            //获取href。


            $temp = pq("img", $value)->attr("src"); //图片链接
            echo $temp;
            $ext = ".png";
            $status = strripos($temp, ".png");
            if ($status != false) {
                $ext = ".png";
            }
            $status = strripos($temp, ".jpg");
            if ($status != false) {
                $ext = ".jpg";
            }
            $status = strripos($temp, ".gif");
            if ($status != false) {
                $ext = ".gif";
            }

            //下载图片
            $url = $temp;
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            $imageData = curl_exec($curl);
            $hash = md5($url);
            curl_close($curl);

            $tp = fopen("K:/png/" . $hash . $ext, "a");
            fwrite($tp, $imageData);
            fclose($tp);
        }
    }


    public function actionTest()
    {
        ini_set('max_execution_time', '0');


        //写入 url ();
        while (1) {
            $this->getData();
            sleep(0.01);
            echo 1;
        }

        //页面爬取完成：下一次，读取一条数据库未读url();

        //设置已查找；
    }

    function getData()
    {

        for ($num = 1; $num < 10; $num++) {

            phpQuery::newDocumentFile('https://coinmarketcap.com/' . $num);//加载页面
            //能否找到 此条url .是否已查找 。已查找一条未查的数据，开始查找。
            //如果能，找过了
            //所有的链接
            $log = array();
            $log['yes'] = 0;
            $log['no'] = 0;
            foreach (pq("#currencies tbody tr ") as $key => $value) {
                //获取href。
                $temp = pq($value);
                //  echo $temp;
                $data = array();
                $temp = pq(".currency-symbol", $value)->text();
                //  $temp = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $temp);
                $data['short_name'] = $this->myTrim($temp);

                $temp = pq(".currency-name-container", $value)->text();

                $data['name'] = $this->myTrim($temp);

                $temp = pq(".market-cap", $value)->text();    //市值
                $temp = $this->number($temp);
                $data['market_cap'] = $this->myTrim($temp);


                $temp = pq(".price", $value)->text();
                $data['usd_price'] = $this->myTrim($temp);

                $temp = pq(".volume", $value)->text();
                $temp = $this->number($temp);
                $data['volume'] = $this->myTrim($temp);

                $temp = pq(".circulating-supply", $value)->text(); //供应

                $temp = $this->myTrim($temp);
                $data['supply'] = $temp;

                $temp = pq(".text-center", $value)->text();    //排名
                $data['ranking'] = $temp;

                $temp = pq("img", $value)->attr("src"); //图片链接
                $data['sparkline'] = $temp;

                $temp = pq(".percent-24h", $value)->text();
                $temp = $this->myTrim($temp);
                $data['day_change'] = $temp;

                $result = DigitalCashLog::model()->add($data);    //是
                $log['time'] = time();
                $log['num'] = $num;
                if ($result['errno'] == 0) {
                    $log['yes'] += 1;    //
                } else {
                    $log['no'] += 1;    //
                    $log['data'] = $data;
                }
                // $temp = htmlspecialchars_decode($temp);
                // $temp = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $temp);
                // $temp = $this->myTrim($temp);    //处理掉美元符号与逗号
                //echo $temp;

            }
            file_put_contents(Yii::app()->getRuntimePath() . '/digital_' . date("Y_W") . '.txt', sprintf("%s %s %s", date('Y-m-d H:i:s'), CJSON::encode($log), PHP_EOL), FILE_APPEND);
        }

    }


    function myTrim($str)
    {
        $search = array(" ", "　", "\n", "\r", "\t", "$", ",", "，", "%");
        $replace = array("", "", "", "", "");
        return str_replace($search, $replace, $str);
    }

    function number($str)
    {
        return preg_replace('/[^\.0123456789]/s', '', $str);
    }

}
